<!--  -->
<template>
  <div>
    <nav-bar>
      <div slot="left" @click="backClick"><icon icon="#icon-fanhui" /></div>
    </nav-bar>
    <div class="info">
      <div class="left"><img :src="cover" alt="" /></div>
      <div class="right">
        <span>{{ name }}</span>
        <span>简介：{{ briefDesc }}</span>
      </div>
    </div>
    <div class="song-list" :style="{ bottom: bottom + 'px' }">
      <p>播放全部</p>
      <b-scroll
        class="wrapper"
        :probe-type="0"
        ref="scroll"
        :pull-up-load="true"
        @pullingUp="loadMore"
      >
        <div>
          <songs-list :data="songList"></songs-list>
          <div class="ls-load" v-show="isShow">正在加载</div>
        </div>
      </b-scroll>
    </div>
  </div>
</template>

<script>
import SongsList from "@/components/content/songList/SongsList";
import NavBar from "@/components/common/navBar/NavBar";
import Icon from "@/components/content/icon/Icon";
import BScroll from "@/components/common/scroll/BScroll";
import { getArtistDetail } from "@/network/artist.js";
import {
  getArtistSongs,
  setSongList,
  getSongDetail,
  getSongUrl,
} from "@/network/songList.js";
export default {
  name: "ArtistDetail",
  data() {
    return {
      id: 0,
      name: "",
      briefDesc: "",
      cover: "",
      page: 0,
      songList: [],
      isShow: false,
      bottom: 0,
    };
  },
  components: {
    Icon,
    NavBar,
    BScroll,
    SongsList,
  },
  created() {
    this.id = this.$route.params.id;
    this.getArtistDetail(this.id);
    this.getArtistSongs(this.id);
  },
  computed: {
    getPlayLength() {
      return this.$store.state.songList.length;
    },
  },
  watch: {
    getPlayLength() {
      if (this.getPlayLength !== 0) {
        this.bottom = 49;
      } else {
        this.bottom = 0;
      }
    },
    songList() {
      this.getSongDetail(this.songList);
      this.getSongUrl(this.songList);
    },
    $route() {
      this.id = this.$route.params.id;
      this.getArtistDetail(this.id);
      this.getArtistSongs(this.id);
    },
  },
  updated() {
    this.$refs.scroll.bs.refresh();
  },
  methods: {
    backClick() {
      this.$router.go(-1);
    },
    loadMore() {
      this.isShow = true;
      setTimeout(() => {
        this.page++;
        this.getArtistSongs(this.id, "hot", 30, this.page * 30);
        this.isShow = false;
      }, 1000);
    },
    getArtistDetail(id) {
      getArtistDetail(id).then((res) => {
        this.name = res.data.artist.name;
        this.briefDesc = res.data.artist.briefDesc;
        this.cover = res.data.artist.cover;
      });
    },
    getArtistSongs(id, order, limit = 30, offset) {
      getArtistSongs(id, order, limit, offset).then((res) => {
        for (let i of res.songs) {
          this.songList.push(new setSongList(i));
        }
      });
    },
    getSongDetail(data) {
      for (let i in data) {
        getSongDetail(data[i].id).then((res) => {
          console.log(res);
          this.songList[i].imgUrl = res.songs[0].al.picUrl;
        });
      }
    },
    getSongUrl(data) {
      for (let i in data) {
        getSongUrl(data[i].id).then((res) => {
          this.songList[i].mp3Url = res.data[0].url;
        });
      }
    },
  },
};
</script>
<style lang='scss' scoped>
.info {
  display: flex;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 75%;
  margin-top: 44px;
  .left {
    width: 80px;
    height: 80px;
    margin: 10px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 2px;
    }
  }
  .right {
    width: 60%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin: 20px 0;
    span:nth-child(2) {
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }
}
.song-list {
  position: fixed;
  top: 25%;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 10px 10px 0 0;
  background: $color-highlight-background;
  p {
    padding: 5% 10px;
  }
  .wrapper {
    height: 90%;
    overflow: hidden;
    border-top: 1px solid rgb(97, 97, 97);
    .ls-load {
      padding: 10px;
      text-align: center;
    }
  }
}
</style>